package com.baihan.angus.app.game.basic.base.view_model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.OnLifecycleEvent;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.baihan.angus.app.game.basic.base.BaseActivity;

public abstract class BaseViewModel extends AndroidViewModel implements LifecycleObserver {

    protected LifecycleProvider mLifecycleProvider;
    protected MutableLiveData<BaseMldEvent> mBaseMld = new MutableLiveData<>();

    public BaseViewModel(@NonNull Application application) {
        super(application);
    }

    @NonNull
    @Override
    public <T extends Application> T getApplication() {
        return super.getApplication();
    }

    public void initLifecycleProvider(LifecycleProvider lifecycleProvider){
        this.mLifecycleProvider = lifecycleProvider;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    protected void onCreate() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    protected void onStart() {
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    protected void onDestroy() {
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    protected void onResume() {
    }

    public MutableLiveData<BaseMldEvent> getBaseMld() {
        return mBaseMld;
    }

    protected void showLoading(String tag){
        mBaseMld.setValue(new BaseMldEvent(BaseActivity.SHOW_LOADING,tag));
    }

    protected void hideLoading(String tag){
        mBaseMld.setValue(new BaseMldEvent(BaseActivity.HIDE_LOADING,tag));
    }

    protected void finish(){
        mBaseMld.setValue(new BaseMldEvent(BaseActivity.FINISH));
    }

    protected void finishWithResult(int activityResult){
        mBaseMld.setValue(new BaseMldEvent(BaseActivity.FINISH_WITH_RESULT,activityResult));
    }
}
